<template>
  <div class="cartcontrol">
    <transition name="move">
      <div class="cart-decrease" v-show="food.count>0" @click.stop="decreaseCart">
        <span class="inner icon-remove_circle_outline"></span>
      </div>
    </transition>
    <div class="cart-count" v-show="food.count>0">{{food.count}}</div>
    <div class="cart-add icon-add_circle" ref="addButtonElement" @click.stop="addCart"></div>
  </div>
</template>

<script>
import Vue from 'vue';
export default {
  props: {
    food: {
      type: Object,
      default() {

      }
    }
  },
  methods: {
    addCart(e) {
      // if (!e._constructed) {
      //   return;
      // }
      console.log('add button clicked!');
      // console.log(e);
      // console.log(e.target);
      if (!this.food.count) { // 如果food.count不存在，就将其值置为1
        // this.food.count = 1;  错误写法
        Vue.set(this.food, 'count', 1); // 向响应式对象中添加一个属性，并确保这个新属性同样是响应式的，且触发视图更新。
      } else {
        this.food.count++;
      }
      this.$emit('getAddButtonElements', e.target);
    },
    decreaseCart() {
      console.log('decrease button clicked!');
      if (this.food.count) {
        this.food.count--;
      }
    }
  }
};
</script>

<style lang="stylus">
  .cartcontrol
    font-size 0px
    .cart-decrease
      display inline-block
      padding 6px
      &.move-enter-active,&.move-leave-active
        transition all 0.4s linear
      .inner
        display inline-block
        line-height 24px
        font-size 24px
        color rgb(240,20,20)
        transition all 0.4s linear
        transform rotate(0deg)
      &.move-enter,&.move-leave-to
        opacity 0
        transform translate3d(24px,0,0)
        .inner
          transform rotate(180deg)
    .cart-count
      display inline-block
      vertical-align top
      width 12px
      padding-top 6px
      line-height 24px
      text-align center
      font-size 16px
      color rgb(147,153,159)
    .cart-add
      display inline-block
      padding 6px
      line-height 24px
      font-size 24px
      color #389fdb
</style>
